package com.ltedu.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ltedu.common.contants.Constants;
import com.ltedu.course.domain.Category;
import com.ltedu.course.domain.Course;
import com.ltedu.course.domain.dto.CategoryNode;
import com.ltedu.course.mapper.CategoryMapper;
import com.ltedu.course.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltedu.course.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 类别表 服务实现类
 * </p>
 *
 * @author Jerry
 * @since 2020-11-26
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CourseService courseService;

    /**
     * 获取一级分类列表
     *
     * @return {@link List<Category>}
     */
    @Override
    public List<Category> getOneLevelCategory() {
        // 获取有效的一级分类
        return this.baseMapper.selectList(new LambdaQueryWrapper<Category>().
                eq(Category::getState, Constants.NORMAL).
                eq(Category::getParentId, 0));
    }

    /**
     * 分类树列表
     *
     * @return {@link List<CategoryNode>}
     */
    @Override
    public List<CategoryNode> treeList() {
//        List<CategoryNode> oneLevelCategoryNodeList = new ArrayList<>();
//
//        // 获取一级分类
//        QueryWrapper<Category> wrapperOne = new QueryWrapper<>();
//        List<Category> oneLevelCategoryList = this.baseMapper.selectList(wrapperOne.
//                eq("parent_id", 0));
//
//        // 获取二级分类
//        QueryWrapper<Category> wrapperTwo = new QueryWrapper<>();
//        List<Category> twoLevelCategoryList = this.baseMapper.selectList(wrapperTwo.
//                ne("parent_id", 0));
//        for (Category category : oneLevelCategoryList) {
//            CategoryNode categoryDTO = new CategoryNode();
//            BeanUtils.copyProperties(category, categoryDTO);
//            oneLevelCategoryNodeList.add(categoryDTO);
//
//            List<CategoryNode> twoLevelCategoryNodeList = new ArrayList<>();
//            for (Category cat : twoLevelCategoryList) {
//                if (cat.getParentId().equals(category.getId())) {
//                    CategoryNode catDTO = new CategoryNode();
//                    BeanUtils.copyProperties(cat, catDTO);
//                    twoLevelCategoryNodeList.add(catDTO);
//                }
//            }
//            categoryDTO.setChildren(twoLevelCategoryNodeList);
//        }
//        return oneLevelCategoryNodeList;
        // 获取有效分类
        List<Category> categoryList = this.baseMapper.selectList(null);
        // 筛选分类并转换为CategoryNode
        List<CategoryNode> categoryNodeList = categoryList.stream()
                .filter(category -> category.getParentId().equals(0L))
                .map(category -> convertNode(category, categoryList))
                .collect(Collectors.toList());
        return categoryNodeList;
    }

    /**
     * 转换节点
     *
     * @param category     类别
     * @param categoryList 类别列表
     * @return {@link CategoryNode}
     */
    private CategoryNode convertNode(Category category, List<Category> categoryList) {
        CategoryNode node = new CategoryNode();
        BeanUtils.copyProperties(category, node);
        List<CategoryNode> categoryNodeList = categoryList.stream()
                .filter(subCategory -> subCategory.getParentId().equals(category.getId()))
                .map(subCategory -> convertNode(subCategory, categoryList))
                .collect(Collectors.toList());
        node.setChildren(categoryNodeList);
        return node;
    }

    /**
     * 是否包含子分类
     *
     * @param id id
     * @return {@link Boolean}
     */
    @Override
    public Boolean hasChildById(Long id) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<Category>().
                eq(Category::getParentId, id)) > 0;
    }

    /**
     * 检查类别是否存在课程中
     *
     * @param id id
     * @return {@link Boolean}
     */
    @Override
    public Boolean checkCategoryExistCourse(Long id) {
        return courseService.count(new LambdaQueryWrapper<Course>().
                eq(Course::getCategoryId, id)) > 0;
    }

    /**
     * 根据上级分类id查询下级分类列表
     *
     * @param parentId 父id
     * @return {@link List<Category>}
     */
    @Override
    public List<Category> getTwoLevelCategory(Long parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, parentId);
        queryWrapper.eq(Category::getState, Constants.NORMAL);
        return this.baseMapper.selectList(queryWrapper);
    }
}
